package com.raincat.multimediapicker;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.raincat.multimediapicker.crop.CropView;
import com.raincat.multimediapicker.utils.ImageCacheUtils;
import com.raincat.multimediapicker.utils.ImageController;
import com.raincat.multimediapicker.utils.ImageItem;

import java.util.ArrayList;

public class ImageCropFragment extends Fragment {
    private Context context;

    private CropView cropView;
    private TextView tv_cancel, tv_sure;

    private ProgressDialog dialog;
    private ImageItem imageItem;

    private ImageCacheUtils imageCacheUtils;

    @SuppressWarnings("deprecation")
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        context = this.getActivity();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.picker_fragment_crop, container, false);
        cropView = (CropView) view.findViewById(R.id.cropView);
        tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        tv_sure = (TextView) view.findViewById(R.id.tv_sure);

        listener();
        return view;
    }

    private void setData() {
        Uri source = Uri.parse("file://" + imageItem.imagePath);
        cropView.of(source).withOutputSize(ImageController.outputX, ImageController.outputY).initialize(context);
    }

    private void listener() {
        tv_cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((PickerActivity) context).fragmentTabAdapter.removeFragment();
            }
        });

        tv_sure.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = ProgressDialog.show(context, null, getString(R.string.picker_now_loading_please_wait));

                new Thread() {
                    public void run() {
                        final Bitmap croppedBitmap = cropView.getOutput();
                        final String key = System.currentTimeMillis() + "";
                        imageCacheUtils = new ImageCacheUtils(context);
                        imageCacheUtils.addBitmapToFile(key, 100, croppedBitmap);

                        ((Activity) context).runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                dialog.dismiss();
                                if (croppedBitmap == null)
                                    Toast.makeText(context, getString(R.string.picker_not_supported), Toast.LENGTH_SHORT).show();
                                else {
                                    ArrayList<String> realPathList = new ArrayList<>();
                                    realPathList.add(imageCacheUtils.getCachePath(key));
                                    ImageController.takeRealPathList();
                                    ((PickerActivity) context).finishActivity("ImageList", realPathList);
                                }
                            }
                        });
                    }
                }.start();
            }
        });
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (imageItem == null) {
            for (ImageItem imageItem : ImageController.imageItemMap.values()) {
                this.imageItem = imageItem;
                break;
            }
            setData();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

}
